package com.cskaoyan.controller.user;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.AdminInfoBean;
import com.cskaoyan.bean.po.InfoData;
import com.cskaoyan.bean.po.LoginUserData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.config.realm.MarketToken;
import com.cskaoyan.controller.system.annotation.RecordOperation;
import com.cskaoyan.service.user.AuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin/auth")
public class AuthController {

    @Autowired
    AuthService authService;

    /**
     * Shiro安全框架 → 视频课程 4个课时 → 认证（登录）
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     *
     * @return
     */
    @RecordOperation
    @PostMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        return authService.login(username,password);
    }



    @RequestMapping("info")
    public BaseRespVo info(String token) {
        return authService.info(token);
    }

    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.succeed("成功");
    }
}
